package com.zrrd.mybatisplusFirst.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.mybatisplusFirst.entity.Person;
import com.zrrd.mybatisplusFirst.service.PersonService;
import com.zrrd.mybatisplusFirst.utils.PageQuery;
import com.zrrd.mybatisplusFirst.utils.ResponseResult;
import com.zrrd.mybatisplusFirst.utils.ResponseStatusCode;
import com.zrrd.mybatisplusFirst.utils.query.AgeQuery;
import com.zrrd.mybatisplusFirst.utils.query.LoginQuery;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author WanJl
 * @version 1.0
 * @title PersonController
 * @description
 * @create 2024/3/23
 */
@RestController
@RequestMapping("/persons")
public class PersonController {
    @Resource
    PersonService personService;

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    @ResponseBody
    public ResponseResult<List> queryAll() {
        return ResponseResult.<List>queryResponse(personService.list());
    }

    /**
     * 添加
     *
     * @param p
     * @return
     */
    @PostMapping
    @ResponseBody
    public ResponseResult<Boolean> add(@RequestBody Person p) {
        return ResponseResult.addResponse(personService.save(p));
    }

    /**
     * 修改
     *
     * @param p
     * @return
     */
    @PutMapping
    @ResponseBody
    public ResponseResult<Boolean> update(@RequestBody Person p) {
        return ResponseResult.updateResponse(personService.updateById(p));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ResponseBody
    public ResponseResult<Boolean> remove(@PathVariable Integer id) {
        return ResponseResult.removeResponse(personService.removeById(id));
    }

    /**
     * @param pageQuery 分页查询条件
     * @return
     */
    @PostMapping("/page")
    @ResponseBody
    public ResponseResult queryPage(@RequestBody PageQuery pageQuery) {
        //1.创建一个IPage分页对象，设置分页参数
        IPage<Person> page = new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize());
        //2.执行分页查询
        page = personService.page(page);

        return ResponseResult.queryResponse(page.getRecords());
    }

    @PostMapping("/queryByCond")
    @ResponseBody
    public ResponseResult queryByCond(@RequestBody Person person) {
        LambdaQueryWrapper<Person> lqw = new LambdaQueryWrapper<>();
        lqw.like(person.getName() != null, Person::getName, person.getName())
                .gt(person.getAge() != null, Person::getAge, person.getAge());

        List<Person> list = personService.list(lqw);

        return ResponseResult.queryResponse(list);
    }

    @PostMapping("/queryByCondOr")
    @ResponseBody
    public ResponseResult queryByCondOr(@RequestBody Person person) {
        LambdaQueryWrapper<Person> lqw = new LambdaQueryWrapper<>();
        lqw.like(person.getName() != null, Person::getName, person.getName())//条件1
                .or()//设置为或条件
                .gt(person.getAge() != null, Person::getAge, person.getAge())//条件2
                .or()
                .lt(person.getAge() != null, Person::getAge, person.getAge());//条件3

        List<Person> list = personService.list(lqw);

        return ResponseResult.queryResponse(list);
    }


    @PostMapping("/queryByCond02")
    @ResponseBody
    public ResponseResult queryByCond(@RequestBody AgeQuery ageQuery) {
        LambdaQueryWrapper<Person> lqw = new LambdaQueryWrapper<>();
        lqw.like(ageQuery.getName() != null, Person::getName, ageQuery.getName())
                .gt(ageQuery.getMinAge() != null, Person::getAge, ageQuery.getMinAge())
                .lt(ageQuery.getMaxAge() != null, Person::getAge, ageQuery.getMaxAge());

        List<Person> list = personService.list(lqw);

        return ResponseResult.queryResponse(list);
    }

    /**
     * 查询指定字段的内容
     *
     * @return
     */
    @PostMapping("/queryAllName")
    @ResponseBody
    public ResponseResult queryAllName() {
        LambdaQueryWrapper<Person> lqw = new LambdaQueryWrapper<>();
        lqw.select(Person::getName);//指定字段 name
        //相当于SQL语句： SELECT name FROM person
        List<Person> list = personService.list(lqw);

        //如果想要抽取Person集合(List<Person>)中所有的姓名，形成一个新的集合(List<String>)
        //方式一：
        List<String> listNames = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Person person = list.get(i);
            listNames.add(person.getName());
        }

        //方式二：通过stream()流获取，(过后统一讲)
        //list.stream().map()

        return ResponseResult.queryResponse(listNames);
    }

    /**
     * 查询总记录数
     *
     * @return
     */
    @PostMapping("/queryCount")
    @ResponseBody
    public ResponseResult queryCount() {

        //方式一：
        int count = personService.count();

        return ResponseResult.queryResponse(count);
    }

    /**
     * 分组查询：按照年龄进行分组，查询每组年龄的人数
     */
    @PostMapping("/queryGroupBy")
    @ResponseBody
    public ResponseResult queryGroupBy() {
        //方式二：
        QueryWrapper<Person> qw = new QueryWrapper<>();
        //SELECT count(*) count,age FROM person
        qw.select("count(*) count,age");
        qw.groupBy("age");
        List<Map<String, Object>> maps = personService.listMaps(qw);
        return ResponseResult.queryResponse(maps);
    }


    @PostMapping("/login")
    @ResponseBody
    public ResponseResult login(@RequestBody LoginQuery loginQuery) {
        LambdaQueryWrapper<Person> lqw = new LambdaQueryWrapper<>();
        lqw.eq(loginQuery.getUsername() != null, Person::getUsername, loginQuery.getUsername());

        Person person = personService.getOne(lqw);
        if (person==null){
            return new ResponseResult<>(ResponseStatusCode.DEFAULT_ERR.getCode(),null,"登录失败");
        }else if (!(person.getPassword().equals(loginQuery.getPassword()))){
            return new ResponseResult<>(ResponseStatusCode.DEFAULT_ERR.getCode(),null,"登录失败");
        }

        person.setUsername("");
        person.setPassword("");
        return new ResponseResult<>(ResponseStatusCode.DEFAULT_OK.getCode(),person,"登录成功");
    }

    /**
     * 使用between完成范围区间查询
     * @param ageQuery
     * @return
     */
    @PostMapping("/queryByCond03")
    @ResponseBody
    public ResponseResult queryByCond03(@RequestBody AgeQuery ageQuery) {
        LambdaQueryWrapper<Person> lqw = new LambdaQueryWrapper<>();
        //大于等于  AND 小于等于
        lqw.between(ageQuery!=null,Person::getAge,ageQuery.getMinAge(),ageQuery.getMaxAge());

        List<Person> list = personService.list(lqw);

        return ResponseResult.queryResponse(list);
    }

    /**
     * 根据id列表批量查询
     * @param list
     * @return
     */
    @PostMapping("/queryByIds")
    @ResponseBody
    public ResponseResult queryByIds(@RequestBody List<Integer> list){
        List<Person> personList = personService.listByIds(list);
        return ResponseResult.queryResponse(personList);
    }
}
